package com.test.singleton.lanhan;

//双重检测
public class Singleton2 {
    private Singleton2(){}
    private static Singleton2 instance = null;

    public static synchronized Singleton2 getInstance(){
        if(instance == null){ //双重检测机制
            synchronized (Singleton2.class){ //同步锁
                if(instance == null){ //双重检测
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }
}
//为什么进行两次判空, 如果a线程已经创建了对象,
// b线程通过了最初的判空,不进行第二次判空的话,还是会创建对象
//同步锁???
